package top.yxqz.servlet;

import top.yxqz.school.SignStudent;
import top.yxqz.school.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author Mr.余
 * @date 2022/11/7
 * @package top.yxqz.servlet
 * <p>
 * 签到的servlet
 */
public class signServletDemo extends HttpServlet {
    public static ArrayList<SignStudent> students = new ArrayList<>();
    //    ip重复的学生
    public static ArrayList<SignStudent> ipStudents = new ArrayList<>();
    //    反复登录的学生
    public static ArrayList<SignStudent> resStudents = new ArrayList<>();
    int id = 1;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        解决乱码
//        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
//        获取ip地址
        String ipAddr = request.getRemoteAddr();

        //        获取前端表单信息.getParameter
        String username = request.getParameter("username");
        String password = request.getParameter("stunumber");

//        创建了学生对象，用来存储从前端传来的表单数据
        SignStudent student = new SignStudent(username, id, new Random().nextInt(25), password, ipAddr);
        int res = repeat(student);
        if (res == 1) {
            System.out.println("该数据存在，【" + student.getName() + "】正在尝试重复签到");
            response.getWriter().print("<h1>老baby，你已经签到了</h1>");
            resStudents.add(student);
        } else {
            //        每次id自增
            id++;
            //       集合存值
            students.add(student);
            response.getWriter().print("签到成功，请好好上课~");
        }

        if (3 == repeat(student)) {
            System.out.println("学生【" + student.getName() + "】具有重复ip问题");
            ipStudents.add(student);
        }


//        每次注册都打印集合中的所有对象信息
        System.out.println("-----------------------------最新签到的结果-----------------------------------");
        for (SignStudent stu : students
        ) {
            System.out.println(stu);
        }
        for (SignStudent stu : ipStudents
        ) {
            System.out.println("具有IP相同问题学生："+stu.getName());
        }

    }

    /**
     * 处理重复问题
     *
     * @param student
     * @return
     */
    private int repeat(SignStudent student) {
        int number = 0;
        String ipAddr = null;
        for (SignStudent stu : students
        ) {
//            判断是否存在相同的学生姓名
            if (stu.getName().equals(student.getName()))
                return number = 1;
//            判断ip是否存在以及该学生是否存在
            if (stu.getIpAddr().equals(student.getIpAddr()) && stu.getName().equals(student.getName()))
                return number = 2;
//            判断该学生ip重复
            if (stu.getIpAddr().equals(student.getIpAddr()))
                return number = 3;
        }
        return number = -1;
    }
}
